/*     */ package com.zimbra.cs.service.util;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.soap.DocumentHandler;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.PrintStream;
/*     */ import java.io.Serializable;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ItemId
/*     */   implements Serializable
/*     */ {
/*     */   private static final long serialVersionUID = -9044615129495573523L;
/*     */   private static final char ACCOUNT_DELIMITER = ':';
/*     */   private static final char PART_DELIMITER = '-';
/*     */   private String mAccountId;
/*     */   private int mId;
/*  51 */   private int mSubpartId = -1;
/*     */   
/*     */   public ItemId(MailItem item) {
/*  54 */     this(item.getMailbox(), item.getId());
/*     */   }
/*     */   
/*     */   public ItemId(Mailbox mbox, int id) {
/*  58 */     this(mbox.getAccountId(), id);
/*     */   }
/*     */   
/*     */   public ItemId(String acctId, int id) {
/*  62 */     this.mAccountId = acctId;this.mId = id;
/*     */   }
/*     */   
/*     */   public ItemId(MailItem item, int subId) {
/*  66 */     this(item.getMailbox().getAccountId(), item.getId(), subId);
/*     */   }
/*     */   
/*     */   public ItemId(String acctId, int id, int subId) {
/*  70 */     this.mAccountId = acctId;this.mId = id;this.mSubpartId = subId;
/*     */   }
/*     */   
/*     */   public ItemId(String encoded, ZimbraSoapContext zsc) throws ServiceException {
/*  74 */     this(encoded, zsc.getRequestedAccountId());
/*     */   }
/*     */   
/*     */   public ItemId(String encoded, String defaultAccountId) throws ServiceException {
/*  78 */     if ((encoded == null) || (encoded.equals(""))) {
/*  79 */       throw ServiceException.INVALID_REQUEST("empty/missing item ID", null);
/*     */     }
/*     */     
/*  82 */     int delimiter = encoded.indexOf(':');
/*  83 */     if ((delimiter == 0) || (delimiter == encoded.length() - 1))
/*  84 */       throw ServiceException.INVALID_REQUEST("malformed item ID: " + encoded, null);
/*  85 */     if (delimiter != -1) {
/*  86 */       this.mAccountId = encoded.substring(0, delimiter);
/*  87 */     } else if (defaultAccountId != null)
/*  88 */       this.mAccountId = defaultAccountId;
/*  89 */     encoded = encoded.substring(delimiter + 1);
/*     */     
/*     */ 
/*  92 */     delimiter = encoded.indexOf('-');
/*  93 */     if (delimiter == encoded.length() - 1)
/*  94 */       throw ServiceException.INVALID_REQUEST("malformed item ID: " + encoded, null);
/*     */     try {
/*  96 */       if (delimiter > 0) {
/*  97 */         this.mSubpartId = Integer.parseInt(encoded.substring(delimiter + 1));
/*  98 */         if (this.mSubpartId < 0)
/*  99 */           throw ServiceException.INVALID_REQUEST("malformed item ID: " + encoded, null);
/* 100 */         encoded = encoded.substring(0, delimiter);
/*     */       }
/* 102 */       this.mId = Integer.parseInt(encoded);
/*     */     } catch (NumberFormatException nfe) {
/* 104 */       throw ServiceException.INVALID_REQUEST("malformed item ID: " + encoded, nfe);
/*     */     }
/*     */   }
/*     */   
/*     */   public static ItemId createFromEncoded(String encoded, String defaultAccountId) throws ServiceException {
/* 109 */     return new ItemId(encoded, defaultAccountId);
/*     */   }
/*     */   
/* 112 */   public String getAccountId() { return this.mAccountId; }
/* 113 */   public int getId() { return this.mId; }
/* 114 */   public int getSubpartId() { return this.mSubpartId; }
/*     */   
/* 116 */   public boolean hasSubpart() { return this.mSubpartId >= 0; }
/*     */   
/*     */   public boolean isLocal() throws ServiceException
/*     */   {
/* 120 */     if (this.mAccountId == null)
/* 121 */       return true;
/* 122 */     Account acctTarget = Provisioning.getInstance().get(Key.AccountBy.id, this.mAccountId);
/* 123 */     if (acctTarget == null)
/* 124 */       throw AccountServiceException.NO_SUCH_ACCOUNT(this.mAccountId);
/* 125 */     return DocumentHandler.getLocalHost().equalsIgnoreCase(acctTarget.getAttr("zimbraMailHost"));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean belongsTo(Account acct)
/*     */   {
/* 132 */     return (acct == null) || (this.mAccountId == null) || (this.mAccountId.equals(acct.getId()));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean belongsTo(String acctId)
/*     */   {
/* 139 */     return (acctId == null) || (this.mAccountId == null) || (this.mAccountId.equals(acctId));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean belongsTo(Mailbox mbox)
/*     */   {
/* 146 */     return (mbox == null) || (this.mAccountId == null) || (this.mAccountId.equals(mbox.getAccountId()));
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 151 */     return toString((String)null);
/*     */   }
/*     */   
/*     */   public String toString(Account authAccount) {
/* 155 */     return toString(authAccount == null ? null : authAccount.getId());
/*     */   }
/*     */   
/*     */   public String toString(ItemIdFormatter ifmt) {
/* 159 */     return toString(ifmt == null ? null : ifmt.getAuthenticatedId());
/*     */   }
/*     */   
/*     */   public String toString(String authAccountId) {
/* 163 */     StringBuffer sb = new StringBuffer();
/* 164 */     if ((this.mAccountId != null) && (this.mAccountId.length() > 0) && (!this.mAccountId.equals(authAccountId)))
/* 165 */       sb.append(this.mAccountId).append(':');
/* 166 */     sb.append(this.mId);
/* 167 */     if (hasSubpart())
/* 168 */       sb.append('-').append(this.mSubpartId);
/* 169 */     return sb.toString();
/*     */   }
/*     */   
/*     */   public boolean equals(Object that) {
/* 173 */     if (this == that)
/* 174 */       return true;
/* 175 */     if (!(that instanceof ItemId)) {
/* 176 */       return false;
/*     */     }
/* 178 */     ItemId other = (ItemId)that;
/* 179 */     if ((this.mAccountId == other.mAccountId) || ((this.mAccountId != null) && (this.mAccountId.equalsIgnoreCase(other.mAccountId)))) {
/* 180 */       return (other.mId == this.mId) && (other.mSubpartId == this.mSubpartId);
/*     */     }
/* 182 */     return false;
/*     */   }
/*     */   
/*     */   public int hashCode() {
/* 186 */     return (this.mAccountId == null ? 0 : this.mAccountId.hashCode()) ^ this.mId;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Map<String, List<Integer>> groupFoldersByAccount(OperationContext octxt, Mailbox mbox, List<ItemId> folderIids)
/*     */     throws ServiceException
/*     */   {
/* 196 */     Map<String, List<Integer>> foldersMap = new HashMap();
/* 197 */     for (ItemId iidFolder : folderIids) {
/* 198 */       String targetAccountId = iidFolder.getAccountId();
/* 199 */       int folderId = iidFolder.getId();
/*     */       try {
/* 201 */         if (mbox.getAccountId().equals(targetAccountId)) {
/* 202 */           boolean isMountpoint = true;
/* 203 */           int hopCount = 0;
/*     */           
/* 205 */           while ((isMountpoint) && (hopCount < 5)) {
/* 206 */             Folder folder = mbox.getFolderById(octxt, folderId);
/* 207 */             isMountpoint = folder instanceof Mountpoint;
/* 208 */             if (isMountpoint) {
/* 209 */               Mountpoint mp = (Mountpoint)folder;
/* 210 */               folderId = mp.getRemoteId();
/* 211 */               if (!mp.isLocal())
/*     */               {
/* 213 */                 targetAccountId = mp.getOwnerId();
/* 214 */                 Account targetAcct = Provisioning.getInstance().get(Key.AccountBy.id, targetAccountId);
/* 215 */                 if (targetAcct != null) break;
/* 216 */                 throw MailServiceException.NO_SUCH_MOUNTPOINT(mp.getId(), mp.getOwnerId(), mp.getRemoteId(), AccountServiceException.NO_SUCH_ACCOUNT(targetAccountId));
/*     */               }
/*     */               
/*     */ 
/*     */ 
/*     */ 
/* 222 */               hopCount++;
/*     */             }
/*     */           }
/* 225 */           if (hopCount >= 5)
/* 226 */             throw MailServiceException.TOO_MANY_HOPS(iidFolder);
/*     */         }
/* 228 */         List<Integer> folderList = (List)foldersMap.get(targetAccountId);
/* 229 */         if (folderList == null) {
/* 230 */           folderList = new ArrayList();
/* 231 */           foldersMap.put(targetAccountId, folderList);
/*     */         }
/* 233 */         folderList.add(Integer.valueOf(folderId));
/*     */       } catch (ServiceException e) {
/* 235 */         String ecode = e.getCode();
/* 236 */         ItemIdFormatter ifmt = new ItemIdFormatter(targetAccountId, targetAccountId, false);
/* 237 */         if (ecode.equals("service.PERM_DENIED"))
/*     */         {
/* 239 */           ZimbraLog.calendar.warn("Ignorable permission error " + ifmt.formatItemId(folderId), e);
/*     */         }
/* 241 */         else if (ecode.equals("mail.NO_SUCH_FOLDER"))
/*     */         {
/* 243 */           ZimbraLog.calendar.warn("Ignoring deleted folder " + ifmt.formatItemId(folderId));
/*     */         }
/*     */         else {
/* 246 */           throw e;
/*     */         }
/*     */       }
/*     */     }
/* 250 */     return foldersMap;
/*     */   }
/*     */   
/*     */   public static void main(String[] args) {
/* 254 */     ItemId foo = null;
/*     */     try {
/* 256 */       foo = new ItemId("34480-508bc90b-d85e-45d6-bca2-7c34b7c407cb:34479", (String)null);
/*     */     } catch (ServiceException e) {
/* 258 */       e.printStackTrace();
/*     */     }
/* 260 */     System.out.println(foo.toString());
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/util/ItemId.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */